﻿namespace SafetyRecordData.Data.DAL
{
    using System;
    using System.Data;
    using System.Data.Common;
    using System.IO;
    using System.Web;
    using Ths.Components.Common;
    using Ths.Components.Excel;
    using Wuqi.Webdiyer;

    [Serializable]
    public class DASafePraise : DBBase
    {
        private const string SafePraise_DefaultList_DCOUNT = "select count(*)  FROM SafePraise {0}";
        private const string SafePraise_DefaultList_DSQL = "\r\n\t\tSELECT * FROM (\r\n\t\t    SELECT row_number() OVER({0}) as ROW_NUMBER,* FROM \r\n\t\t    ( \r\n\t\t        SELECT  [AppID] AS [AppID],  [Company] AS [Company],  [InCompany] AS [InCompany],  [WorkerNumber] AS [WorkerNumber],  [WorkerName] AS [WorkerName],  [PraiseTime] AS [PraiseTime],  [PraiseReason] AS [PraiseReason],  [PraiseContent] AS [PraiseContent],  [id] AS [id]  FROM [SafePraise]\r\n\t\t        {1}\r\n\t\t    ) as a\r\n\t\t) as b WHERE ROW_NUMBER > {2} AND ROW_NUMBER <= {3}";

        public DASafePraise()
        {
            base.TableName = "SafePraise";
            base.PKField = "AppID";
        }

        public DASafePraise(string connectionStringName) : base(connectionStringName)
        {
            base.TableName = "SafePraise";
            base.PKField = "AppID";
        }

        public DataTable GetHurtListCount(DbParameter[] parameters, string orderBy, string where)
        {
            DataTable dt = base.DBFactory.GetDataTable("proc_SafePraise", CommandType.StoredProcedure, parameters);
            ExcelEntity _excel = new ExcelEntity();
            _excel.Sql = string.Format(" exec proc_SafePraiseExcel " + where, new object[] { orderBy, where, 0, 100 });
            _excel.Parameters = parameters;
            HttpContext.Current.Session[Path.GetFileName(HttpContext.Current.Request["Url"].ToString())] = _excel;
            return dt;
        }

        public DataTable GetHurtListDetailCount(DbParameter[] parameters, string orderBy, string where)
        {
            DataTable dt = base.DBFactory.GetDataTable("proc_SafePraiseDetail", CommandType.StoredProcedure, parameters);
            ExcelEntity _excel = new ExcelEntity();
            _excel.Sql = string.Format(" exec proc_SafePraiseDetailExcel " + where, new object[] { orderBy, where, 0, 100 });
            _excel.Parameters = parameters;
            HttpContext.Current.Session[Path.GetFileName(HttpContext.Current.Request["Url"].ToString())] = _excel;
            return dt;
        }

        public DataTable SafePraise_DefaultListPR(AspNetPager AspNetPager1, DbParameter[] parameters, string orderBy, string where)
        {
            string sql = string.Format("select count(*)  FROM SafePraise {0}", where);
            CommonClass.InitAspPageInfo(CommonUtility.Get_Int32(base.DBFactory.ExecuteScalar(sql, CommandType.Text, parameters)), AspNetPager1);
            sql = string.Format("\r\n\t\tSELECT * FROM (\r\n\t\t    SELECT row_number() OVER({0}) as ROW_NUMBER,* FROM \r\n\t\t    ( \r\n\t\t        SELECT  [AppID] AS [AppID],  [Company] AS [Company],  [InCompany] AS [InCompany],  [WorkerNumber] AS [WorkerNumber],  [WorkerName] AS [WorkerName],  [PraiseTime] AS [PraiseTime],  [PraiseReason] AS [PraiseReason],  [PraiseContent] AS [PraiseContent],  [id] AS [id]  FROM [SafePraise]\r\n\t\t        {1}\r\n\t\t    ) as a\r\n\t\t) as b WHERE ROW_NUMBER > {2} AND ROW_NUMBER <= {3}", new object[] { orderBy, where, (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize });
            ExcelEntity _excel = new ExcelEntity();
            _excel.Sql = string.Format("\r\n\t\tSELECT * FROM (\r\n\t\t    SELECT row_number() OVER({0}) as ROW_NUMBER,* FROM \r\n\t\t    ( \r\n\t\t        SELECT  [AppID] AS [AppID],  [Company] AS [Company],  [InCompany] AS [InCompany],  [WorkerNumber] AS [WorkerNumber],  [WorkerName] AS [WorkerName],  [PraiseTime] AS [PraiseTime],  [PraiseReason] AS [PraiseReason],  [PraiseContent] AS [PraiseContent],  [id] AS [id]  FROM [SafePraise]\r\n\t\t        {1}\r\n\t\t    ) as a\r\n\t\t) as b WHERE ROW_NUMBER > {2} AND ROW_NUMBER <= {3}", new object[] { orderBy, where, 0, AspNetPager1.RecordCount });
            _excel.Parameters = parameters;
            HttpContext.Current.Session[Path.GetFileName(HttpContext.Current.Request["Url"].ToString())] = _excel;
            DataTable dt = base.DBFactory.GetDataTable(sql, CommandType.Text, parameters);
            return base.FullRow(dt, AspNetPager1.PageSize);
        }
    }
}

